PIZZA Current compiler version: 0.39d
A substantial companion to Java
Introduction to Java and Pizza

Contents
Home
Mirrors
FAQ

Distribution
Support
Documents
-Tutorial index
-Examples
-API
-Definition

Applications
Users

People
Links

Week 1: Java Fundamentals


Java is…


Applet vs Application

  • Application: normal Java program
  • Applet: program to be executed by a web browser
  • Applets are typically loaded from some URL on web before they are executed.
  • Applets are the "new thing" which made Java popular.

  • Security


    Security: Language


    Security: Verifier


    Security: Runtime

            public boolean delete() {
                SecurityManager security = 
                     System.getSecurityManager();
                if (security != null) {
                    security.checkDelete(path);
                }
                return delete0();
            }
    


    Java vs C++


    New Features in Java


    Advantages of Java vs C++


    Disadvantages of Java vs C++


    Hello World Application

            /** 
             * a typical hello world! program.
             */
            class HelloWorld { 
    
                /** the main method is executed by the command
                 *
                 * java HelloWord
                 */
                 public static void main(String[] argv) {
                     System.out.println("hello world!");
                 }
            }
    
    
    
    


    Executing Java Programs

            java HelloWord
    
    
            static public void main(String args[])
    
    


    The Class Path

            /usr/local/java/classes:/usr/local/lib/classes.zip
    
    


    Best way to access your own (and other) classes

            setenv CLASSPATH $HOME/classes:$CLASSPATH
    
    
            alias jc javac -d ~/classes
            alias pc java -ms8m pizza.compiler.Main -d ~/classes
    
    
    


    Compiling Java Programs

    If you use javac:

            javac HelloWorld.java
    
    

    or, if you define the alias:

            jc HelloWord.java
    
    

    If you want to use pizza instead, always make an alias, and use:

            pc HelloWord.java
    
    

    Finding out about options:

            pc
    
    


    No Linking!


    Primitive Datatypes

    Signed integers:

    Characters:

    Operators:

            ==, !=, <, >, <=, >=, +, -, *, /, %, ++, --,
             <<, >>, >>>, ~, &, |, ^
    
    


    Primitive Datatypes

    Floating point values:

    Format is IEEE 754

    Operators:

            ==, !=, <, >, <=, >=, +, -, *, /, %, ++, --
    
    

    No floating point exceptions, overflows yield NaN.


    Primitive Datatypes

    Boolean type:

    Operators:


    Primitive Datatypes

            byte < short
                          < int < long < float < double
                    char
    
    
            long x = (long)2       // (long) redundant
    
    
            byte x = (byte)257     // x == 1 
    
    
            b ? 1 : 0      and      x != 0
    
    


    Class Declarations

            class ASimpleClass {
                int aVariable;            // field
                boolean aMethod() {       // method
                    if (aVariable == 0) {
                        aVariable++;
                        return true;
                    } else {
                        return false;
                    }
                }
                ASimpleClass() {          // constructor
                    aVariable = 0;
                }
            }
    
    


    Creating Objects


    Accessing Instance Members

    Accessing members of other objects:

            ASimpleClass c = new ASimpleClass();
            c.aMethod();
            System.out.println(c.aVariable);
    
    
    

    Accessing members of own object by unqualified name or using this:

            Class Class1 {
                int field;
                boolean field2;
                Class1(int field) {
                    this.field = field;
                    field2 = true;
                }
            }
    
    


    Static members

            class Class2 {
                static int initialized = false;
                static boolean init()
                     if (!initialized) {
                        initialised = true;
                        System.out.println("init done.");
                     }
                }     
                int field;
                Class2() { init(); }
            }
    
    

    Accessing static members of other classes:

            Class2 obj = new Class2();
            if (!Class2.initialized) obj.field = 1;
    
    
    


    Class Elements

    Instance variables (one per object) int x;
    Static variables (one per class) static int y;
    Instance methods (one per object) void incr_xy() { x ++; y++; }
    Static methods (one per class) static void incr_y() { y++; }
    Constructor methods C() { x = 0; }
    Initializer blocks { x = 0; }
    Static initializer blocks static { y = 0; }
    
    
    


    Parameter Passing


    Statements

            if (cond) S1
            if (cond) S1 else S2
            switch (sel) { case P1: S1 … case Pn: Sn default: Sd }
            while (cond) S
            do S while (cond)
            for (init; step; cond) S
            L: S
            break; break L;
            continue; continue L;
            return; return Expr;
            throw Exception;
    

    Missing:

            goto L;
    
    


    Arrays

    Are always generated dynamically, with new.

            int[] intvec = new int[100];
            double[][] matrix1 = new double[10][20];
            double[][] matrix2 = new double[10][];
            for (int I = 0; I < 10; I++) matrix2[I] = new double[20];
    
    

    Access as in C:

            intvec[99]
            matrix[9]
            matrix[9,19]
    
    

    Additionally, one can find out about the length of an array

            intvec.length;
            matrix.length;
            matrix[0].length;
    
    

    Arrays are conceptually object types.


    Expressions

    Variable and array access: x, a[I]
    class- and instance-member access: C.x, obj.x
    Method calls: m(x, y), o.m(x, y), C.m(x, y)
    Object creation: new C()
    Arithmetic expressions:
    Assignments: a = b;
    Type-test: obj instanceof C
    Type-cast: ( C ) obj, (int)1.0
    Conditional expression: if (x > 0) 1 ? -1


    Packages

            group . project . class
            company . library . class
    
    
            pizza.compiler.Main
            java.lang.System
    
    
    


    Imports

    import java.io.File lets you use File instead of java.io.File
    import java.io.* makes available all files in java.io in unqualified form.


    Always implicitly imported: java.lang


    The Standard Library

    Organized as packages:

    java.lang Basis classes: Object, String, System, Math
    java.io Input/Output
    java.util Utility classes: hash tables, vectors, enumerations
    java.applet Helper classes for applet execution
    java.awt Platform-independent graphical user interface
    java.net Network programming


    Example: jcat


    jcat.java

            import java.io.*;
    
            public class jcat {
                private static final String usage = 
                     "Usage: java jcat -v *";
    
                /** copy file to standard output
                 */
                private static void copy(String filename) {
                    FileInputStream in;
                    try {
                        in = new FileInputStream(filename);
                    } catch (IOException ex) {
                        System.out.println(
                             "jcat: cannot open " + filename);
                        return;
                    }
                    try {
                        // read whole file in at once.
                        byte[] buf = new byte[in.available()];
                        in.read(buf);
                        // send whole buffer to standard output
                        System.out.write(buf, 0, buf.length);
                        in.close();
                    } catch (IOException ex) {
                        System.out.println("jcat: error on file " + 
                                            filename + ": " + ex);
                    }
               }
    
    
    
    


    jcat.java continued

                public static void main(String[] args) {
                     int firstFileNameIndex = 0;
                     boolean verboseMode = false;
                     // check for proper usage
                     if (args.length == 0) {
                         System.err.println(usage);
                         return;
                     } else if (args[0].charAt(0) == '-') {
                         if (args[0].equals("-v")) && args.length > 1) {
                             firstFileNameIndex = 1;
                             verboseMode = true;
                         } else {
                             System.err.println(usage);
                             return;
                         }
                     }
                     // now process all files
                     for (int I = firstFileNameIndex; I < args.length; 
                           I++) {
                         copy(args[I]);
                         if (verboseMode)
                             System.err.println(
                                  "processed file " + args[I]);
                    }
               }
            }
    
    
    
    
    


    Compiling and running jcat

    Compile:

    Run:


    Assignment

            java.lang.Integer
            java.io.DataInputStream
    


    Java is a trademark of Sun Microsystems.
    Comments and bug reports to the Pizza Group, pizza@cis.unisa.edu.au.
    All software and documents on the Pizza site are © Copyright 1996, 1997 by the respective authors (as attributed on each; terms for redistribution are available).